<template>
	<view class="content pl30 pr30 bgc-fff">
		<u-form :model="form" ref="uForm" label-position="top">
			<u-form-item
				required 
				label="门店名称" 
				prop="name"
			>
				<u-input 
					v-model="form.name" 
					placeholder="请输入门店名称" 
				/>
			</u-form-item>
			
 			<u-form-item
				required 
				label="联系人姓名" 
				prop="contactsName"
			>
				<u-input 
					v-model="form.contactsName" 
					placeholder="请输入联系人姓名" 
				/>
			</u-form-item>
			
			<u-form-item
				required 
				label="联系电话" 
				prop="contactsPhone"
			>
				<u-input 
					v-model="form.contactsPhone" 
					placeholder="请输入联系人电话" 
				/>
			</u-form-item>
			
<!-- 			<u-form-item
				required 
				label="营业时间" 
				prop="workTime"
			>
				<u-input 
					v-model="form.workTime"
					placeholder="请输入营业时间" 
				/>
			</u-form-item> -->
			
			<u-form-item 
				required 
				label="门店地址" 
				prop="address" 
				right-icon="map-fill"
			>
<!-- 				<u-input 
					v-model="form.address" 
					disabled 
					placeholder="点我定位所在门店位置"  
					@click="showMap" 
				/> -->
				<view @click="showMap" style="color: #777777;"><text>{{form.address || '点我定位所在门店位置'}}</text></view>
			</u-form-item>
			
<!-- 			<u-form-item
				required 
				label="优惠政策" 
				prop="preferential"
			>
				<u-input 
					v-model="form.preferential" 
					placeholder="请输入优惠政策" 
				/>
			</u-form-item> -->
			
			
			
<!-- 			<u-form-item
				required
				label="排序" 
				prop="sort"
			>
				<u-input 
					v-model="form.sort"
					 type="number"
					placeholder="请输入排序" 
				/>
			</u-form-item> -->
			
			<u-form-item 
				required
				label="门店照片" 
				prop="img"
			>
				<view style="min-height: 220rpx;">
					<imgUpload
						ref="img_upload"
						@changeSuccess="upLoaddImg" 
						@removeImg="removeImg" 
					/>
				</view>
			</u-form-item>
			
			<u-form-item
				label="商家用户id" 
				prop="userId"
			>
				<u-input 
					v-model="form.userId" 
					placeholder="请输入商家用户id" 
				/>
			</u-form-item>
			
			<u-form-item
				label="经销商用户id" 
				prop="dealerId"
			>
				<u-input 
					v-model="form.dealerId" 
					placeholder="请输入经销商用户id" 
				/>
			</u-form-item>
		</u-form>
		
		<footerBottom>
			<view class="pl30 pr30 z-flex z-flex-align-c" style="height: 100%;">
				<view style="width: 100%;">
					<u-button 
						:loading="btnLoading"
						:disabled="btnLoading"
						type="success" 
						:custom-style="customStyle" 
						shape="circle"
						@click="add"
					>
						完成
					</u-button>
				</view>
			</view>
		</footerBottom>
		
		<u-picker 
			mode="time" 
			v-model="hourShow" 
			:params="{year: false,month: false,day: false,hour: true,minute: true,second: false}"
			@confirm="handSelectHour"
		>
		</u-picker>
	</view>
</template>

<script>
	import {
		addStoreApi,getOneApi
	} from '@/api/shop.js'
	export default {
		data() {
			return {
				hourShow: false,
				btnLoading: false,
				labelStyle: {
					fontSize: '30rpx'
				},
				customStyle: {
					background: '#23bbb3'
				},
				form: {
					userId: '',
					dealerId: '',
					name: '',
					contactsName: '',
					contactsPhone: '',
					address: '', // 手动选择的地址
					workTime: '',
					preferential: '',
					sort: 1,
					img: [], // 门店图片
				},
				rules: {
					contactsName: [{
						required: true,
						message: '请输入联系人名称',
						trigger: ['change', 'blur'],
					}],
					contactsPhone: [{
						required: true,
						message: '请输入联系人电话',
						trigger: ['change', 'blur'],
					}],
					address: [{
						required: true,
						message: '请选择门店地址',
						trigger: ['change', 'blur'],
					}],
					name: [{
						required: true,
						message: '请输入门店名称',
						trigger: ['change', 'blur'],
					}],
					img: [{
						validator: (rule, value, callback) => {
							return this.form.img.length != 0
						},
						message: '请上传图片',
						trigger: ['change', 'blur'],
					}]
				},
				option: {}
			};
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		onLoad(option) {
			if(option){
				this.option = option
				if(option.pageType === 'edit'){
					uni.setNavigationBarTitle({
						title: '编辑门店'
					})
					this.getStoreInfo()
				}
			}
		},
		onShow(){
			
		},
		methods: {
			handSelectHour(e){
				const {hour,minute} = e
				this.form.workTime = `${hour}:${minute}`
			},
			showMap() {
				uni.getLocation({
					type: 'gcj02',
					isHighAccuracy: true,
					success: (res) => {
						uni.chooseLocation({
							latitude: res.latitude,
							longitude: res.longitude,
							success: (loca) => {
								this.form.address = loca.address
								this.form.latitude = loca.latitude
								this.form.longitude = loca.longitude
							}
						});
					},
					fail: (err) => {

					}
				});
			},
			upLoaddImg(val) {
				let data = val.filter(i => {
					return i.response && i.response.code == 0
				})
				this.$nextTick(() => {
					this.form.img = data.map(i => i.response.data.url)
				})
			},
			async removeImg(index) {
				this.form.img.splice(index, 1)
			},
			async getStoreInfo(){
				const {code, data} = await getOneApi({id: this.option.id})
				if(code != 0) return
				data.userId = data.userId
				data.dealerId = data.dealerId
				this.form = data
				this.$nextTick(()=>{
					const imgUrl = data.img.map(it=>{return {url: it}})
					this.$refs.img_upload.fileList = imgUrl
				})
			},
			add() {
				this.$refs.uForm.validate(async (valid) => {
					if (valid) {
						this.btnLoading = true
						try {
							let dataInfo = JSON.parse(JSON.stringify(this.form))
							dataInfo.userId = Number(dataInfo.userId)
							dataInfo.dealerId = Number(dataInfo.dealerId)
							if(this.option.pageType == 'add'){
								let {code, data} = await addStoreApi(dataInfo)
								this.btnLoading = false
								if (code != 0) return
								uni.showModal({
									title: '新增成功',
									content: '请等待列表的刷新',
									confirmText: '我知道咯',
									showCancel: false,
									success: (res) => {
										uni.$u.route({
											type: 'navigateBack'
										})
									}
								});
							}else{
								dataInfo.id = Number(this.option.id)
								let {code,data} = await addStoreApi(dataInfo)
								this.btnLoading = false
								if (code != 0) return
								uni.showModal({
									title: '编辑成功',
									content: '请等待列表的刷新',
									confirmText: '我知道咯',
									showCancel: false,
									success: (res) => {
										uni.$u.route({
											type: 'navigateBack'
										})
									}
								});
							}
						} catch (e) {
							this.btnLoading = false
							throw new Error(e)
						}
					} else {
						this.btnLoading = false
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		width: 100vw;
		height: 100vh;
		overflow-y: auto;
	}
</style>
